package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.PointDefend;
import com.example.entity.SpotCheckPlan;
import com.example.mapp.PointFlowMapp;
import com.example.mapp.SpotCheckItemMapp;
import com.example.mapp.SpotCheckPlanMapp;
import com.example.mapp.SpotObjectPlanMapp;
import com.example.service.SpotCheckPlanService;
import com.example.util.DateUtils;
import com.example.util.ResultUtil;
import com.example.util.SystemPointUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 维保人员抽查计划
 */
@Service
public class SpotCheckPlanServiceImpl implements SpotCheckPlanService {

	private final static Logger logger = (Logger) LoggerFactory.getLogger(SpotCheckPlanServiceImpl.class);

	@Autowired
	private SpotCheckPlanMapp spotCheckPlanMapp;
	// 检查清单
	@Autowired
	private SpotCheckItemMapp spotCheckItemMapp;
	// 实施对象清单
	@Autowired
	private SpotObjectPlanMapp spotObjectPlanMapp;

	@Autowired
	private PointFlowMapp pointFlowMapp;

	/**
	 * 查询某一段时间内得维保信息
	 * spotCheckPlan
	 * @return
	 */

	@Override
	public Map<String, Object> findList(SpotCheckPlan spotCheckPlan) {
		String message = "查询成功";
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			Page<SpotCheckPlan> page = PageHelper.startPage(spotCheckPlan.getCurrentPage(), spotCheckPlan.getPageSize());
			List<SpotCheckPlan> list = spotCheckPlanMapp.findList(spotCheckPlan);
			map.put("list", list);
			map.put("total", page.getTotal());
		}catch (Exception e){
			logger.error(e.getMessage());
			message="查询失败";
			return ResultUtil.returnError(message + e.getMessage(),spotCheckPlan);
		}
		return ResultUtil.returnSuccess(message, map);
	}

	/**
	 * 新增数据
	 * @param
	 * @return
	 */
	@Override
	public Map<String, Object> insert(SpotCheckPlan spotCheckPlan) {
		String message = "添加成功";
		Map<String, Object> resultMap = new HashMap<>();
		try {
			spotCheckPlan.setCreateDate(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
			spotCheckPlan.setDelFlag("1");
			spotCheckPlan.setIsCheck("N");   // 是否添加检查项
			spotCheckPlan.setIsSendMessage("N");
			spotCheckPlanMapp.insert(spotCheckPlan);
		}catch (Exception e){
			logger.error(e.getMessage());
			message = "添加失败";
			return ResultUtil.returnError(message + e.getMessage(), spotCheckPlan);
		}
		return ResultUtil.returnSuccess(message, resultMap);
	}

	/**
	 * 修改数据
	 * @param
	 * @return
	 */
	@Override
	public Map<String, Object> update(SpotCheckPlan spotCheckPlan) {
		String message = "修改成功";
		Map<String, Object> resultMap = new HashMap<>();
		try {
			spotCheckPlan.setUpdateDate(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
			spotCheckPlanMapp.update(spotCheckPlan);
		}catch (Exception e){
			logger.error(e.getMessage());
			message = "修改失败";
			return ResultUtil.returnError(message + e.getMessage(), spotCheckPlan);
		}
		return ResultUtil.returnSuccess(message, resultMap);
	}
	/**
	 * 批量删除
	 * @param
	 */
	@Override
	public Map<String,Object> delete(List<String> ids,Long userId)  {
		String message = "删除成功";
		Map<String, Object> resultMap = new HashMap<>();
		try {
			// 批量删除计划
			for(int i=0;i<ids.size();i++){
				spotCheckPlanMapp.delete(ids.get(i));
			}
			// 计划删除后  该计划下的检查清单删除
			for(int i=0;i<ids.size();i++){
				spotCheckItemMapp.deleteByPlanId(Long.valueOf(ids.get(i)));
			}
			// 删除计划后，添加实施对象删除
			for(int i=0;i<ids.size();i++){
				spotObjectPlanMapp.deleteByPlanId(Long.valueOf(ids.get(i)));
			}

		}catch (Exception e){
			logger.error(e.getMessage());
			message = "删除失败";
			return ResultUtil.returnError(message + e.getMessage(), ids);
		}finally {
			// 获取缓存
			if (ids != null && ids.size() > 0) {
				PointDefendInfoCache infoCache = new PointDefendInfoCache();
				PointDefend defend = infoCache.getKeyByValue("maintenance_check_plan");

				// 获取当前用户，计算积分
				Map<String, Object> maps = pointFlowMapp.findUserInfoByUserId(userId);
				long mark = (long) maps.get("mark");
				int round = (int) (mark - (defend.getMark() * ids.size()));

				// 更新用户信息总积分
				String userIds = maps.get("userId").toString();
				int i1 = pointFlowMapp.updateMark(round, userIds);

				String userName = maps.get("userName").toString();
				String partyName = maps.get("partyName").toString();
				SystemPointUtil.insertFlow(pointFlowMapp, i1, defend, round, userName, partyName, "删除" + ids.size() + "份维保计划");
			}
		}
		return ResultUtil.returnSuccess(message, resultMap);
	}
}
